package com.jadows.tdog.service.impl;

import com.aliyun.sdk.service.alimt20181012.AsyncClient;
import com.aliyun.sdk.service.alimt20181012.models.TranslateRequest;
import com.aliyun.sdk.service.alimt20181012.models.TranslateResponse;
import com.aliyun.sdk.service.alimt20181012.models.TranslateResponseBody;
import com.jadows.tdog.service.TranslateService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.CompletableFuture;

@Service
@RequiredArgsConstructor
public class AliTranslateService implements TranslateService {

    final AsyncClient translateClient;


    @Override
    public CompletableFuture<String> asyncTranslate(String text, String from, String to, Map<String, Object> options) {
        TranslateRequest request = TranslateRequest.builder()
                .formatType("text")
                .sourceLanguage(from)
                .targetLanguage(to)
                .sourceText(text)
                .scene("general")
                .build();
        CompletableFuture<TranslateResponse> translate = translateClient.translate(request);
        return translate.thenApply(e -> {
            TranslateResponseBody body = e.getBody();
            TranslateResponseBody.Data data = body.getData();
            return data.getTranslated();
        });
    }
}
